package com.edu.cuotibook.controller;

import com.edu.cuotibook.entity.common.CommonException;
import com.edu.cuotibook.entity.common.ResultDTO;
import com.edu.cuotibook.entity.pojo.Examination;
import com.edu.cuotibook.service.GenerateService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * create by sym  2021/3/21 10:24.
 */

@RestController
@RequestMapping("/CTMain")
@CrossOrigin
@Api(description = "错题本主页接口",value = "Main api")
public class CTMainController {

    @Autowired
    private GenerateService generateService;

    @GetMapping("/showDirs")
    @ApiOperation(value = "显示文件夹路径")
    public ResultDTO<Examination> showDirs(@ApiParam(value = "考试id") @RequestParam("id") Long id) throws InterruptedException {
        return ResultDTO.sucess(generateService.getDirs(id));
    }

    @GetMapping("/setDir")
    @ApiOperation(value = "设置文件夹路径 type=0:题目 type=1:参考答案 type=2:错误答案 type=3:表格 type=4:总报告 type=5:学生报告")
    public ResultDTO setDir
            (@ApiParam(value = "考试id") @RequestParam("id") Long id,
             @ApiParam(value = "业务类型") @RequestParam("type") Integer type,
             @ApiParam(value = "路径") @RequestParam("path")String path) throws CommonException {
        generateService.setDir(id,type,path);
        return ResultDTO.sucess();
    }

    @GetMapping("/showList")
    @ApiOperation(value = "分页查询所有考试信息")
    public ResultDTO getExams
            ( @ApiParam(value = "pageNum") @RequestParam("pageNum") Integer pageNum,
             @ApiParam(value = "pageSize") @RequestParam("pageSize")Integer pageSize) throws CommonException {
        PageInfo page = generateService.getExams(pageNum, pageSize);
        return ResultDTO.sucess(page);
    }

    @PostMapping("/uploadFile")
    @ApiOperation(value = "上传文件 type=0_题目 type=1_参考答案 type=2_错误答案 type=3_表格 ")
    public ResultDTO uploadFile(@RequestParam("type") Integer type,@RequestParam(value = "homedir") String homedir,MultipartFile file) throws CommonException, IOException {
        generateService.uploadFile(type,file,homedir);
        return ResultDTO.sucess();
    }

    @PostMapping("/generatReport")
    @ApiOperation(value = "生成总报告")
    public ResultDTO generatReport(@RequestParam("id")String id) throws IOException {
        String reportDir =  generateService.generatReport(Long.valueOf(id));
        return ResultDTO.sucess(reportDir);
    }

    @PostMapping("/generatReports")
    @ApiOperation(value = "生成分开的报告")
    public ResultDTO generatReports(@RequestParam("id")String id) throws IOException {
        String reportsDir =  generateService.generatReports(Long.valueOf(id));
        return ResultDTO.sucess(reportsDir);
    }

    @PostMapping("/generatReportThree")
    @ApiOperation(value = "所有同学的题目生成后，再生成所有同学的答案")
    public ResultDTO generatReportThree(@RequestParam("id")String id) throws IOException {
        String reportsDir =  generateService.generatReportThree(Long.valueOf(id));
        return ResultDTO.sucess(reportsDir);
    }

    @PostMapping("/deleteExam")
    @ApiOperation(value = "删除考试")
    public ResultDTO deleteExam(@RequestParam("id")String id)
    {
        generateService.deleteExam(Long.valueOf(id));
        return ResultDTO.sucess();
    }

}
